<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-input v-model="form.id" type="hidden"></el-input>
      <el-input v-model="form.dept" type="hidden"></el-input>
      <el-form-item label="用户姓名" prop="own_nickname">
        <el-input v-model="form.own_nickname"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="accname">
        <el-input v-model="form.accname"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobileno">
        <el-input v-model="form.mobileno"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="dept">
        <el-cascader
          v-model="selected"
          :options="deptdata"
          clearable
          @change="handleNodeClick"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="启用状态" prop="enable">
        <el-radio-group v-model="form.enable">
          <el-radio label="1" model-value="enable">启用</el-radio>
          <el-radio label="0" model-value="enable">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio label="1" model-value="sex">男</el-radio>
          <el-radio label="2" model-value="sex">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="注意">
        <el-link type="danger" disabled>
          修改账号时暂不能修改账号、手机号、邮箱
        </el-link>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/sysaccount'

  export default {
    name: 'RoleManagementEdit',
    data() {
      return {
        deptdata: '',
        form: {
          id: '',
          enable: '1',
          sex: '1',
          own_nickname: '',
          dept: '',
          email: '',
          mobileno: '',
          accname: '',
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.getDeptlist()
    },
    methods: {
      async getDeptlist() {
        this.listLoading = true
        const { data } = await getDeptlistTree(this.queryForm)
        this.data = data
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(this.form)
            //this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      handleNodeClick(data) {
        console.log(data)
      },
    },
  }
</script>
